<?php $this->fetch('public/head') ?>
<div class="layui-tab layui-tab-brief">
    <ul class="layui-tab-title">
        <li class="layui-this">成员列表</li>
    </ul>
    <div class="layui-tab-content layui-form">
        <div id="ss" class="layui-form layui-form-pane">
            <div class="layui-inline">
                <label class="layui-form-label">部门</label>
                <div class="layui-input-inline">
                    <select name="depart_id" lay-filter="query" class="query">
                        <?php foreach ($department as $k => $v): ?>
                            <option value="<?php echo $v['id'] ?>" <?php if (input('depart_id') == $k) {
                                echo 'selected="selected"';
                            } ?>><?php echo str_repeat('—', $v['level'] - 1) . $v['name'] ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
        </div>
        <hr>
        <form class="layui-form form-container" action="" method="post">
            <div class="layui-form-item" pane="">
                <label class="layui-form-label">请选择</label>
                <div class="layui-input-block _userlist">

                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="go">添加</button>
                </div>
            </div>
        </form>
    </div>
</div>
<?php $this->fetch('public/script') ?>
<script>
    layui.use(['form'], function () {
        var table = layui.table
            , form = layui.form;
        getLists();
        form.on('select(query)', function (data) {
            $(data.elem).trigger('change', data.elem); //此处可让select支持change事件，trigger方法触发被选元素的指定事件类型。
        });
        //改变查询条件后重新获取列表数据
        $('.query').change(function () {
            getLists();
        });
        function getLists() {
            let params = HS.getParams('.query');
            //数据表格
            $.ajax({
                url: "<?php echo url('getlists')?>",
                data: params,
                success: function (data) {
                    var html = '';
                    if (data.code == 0) {
                        $.each(data.data, function (k, v) {
                            html += '<input type="checkbox" name="userid[]" lay-skin="primary" value="'+v.userid+'" title="' + v.name + '" >'
                        });
                        console.log(html)

                    } else {
                        html = '数据获取失败';
                    }
                    $('._userlist').html(html);
                    form.render();
                }
            })
        }

        form.on('submit(go)', function (data) {
            let loading = layer.load(0); //0代表加载的风格，支持0-2
            /*var form_data = data.field;//$(data.form).serialize()
            form_data.copy_nid = $('#copy_nid').val()
            delete form_data.file;//使用data.field，会生成file的键，删除*/
            $.ajax({
                url: data.form.action,
                type: data.form.method,
                data: $(data.form).serialize(),
                success: function (info) {
                    if (info.code === 1) {
                        setTimeout(function () {
                            layer.close(loading); //关闭加载层
                            window.parent.layer.closeAll();
                            location.href="<?php echo url('put_leader',['id'=>$item['id'],'type'=>1])?>"
                        }, 1000);
                    }
                    layer.msg(info.msg);
                }
            });
            return false;
        });
        //监听复选框选择
        table.on('checkbox(dataList)', function (obj) {
            let checkStatus = table.checkStatus('dataList')
                , data = checkStatus.data;//包含行信息的data二维
            $('.checkedNum').text(data.length)
        });

    });
</script>